﻿// (c) Copyright Clovis Coli Jr.
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;
using System.Windows;

namespace BrasilFramework.Controle.WPF.Comum.Validacao
{
	public abstract class ValidatorService : DependencyObject
	{
		public static ValidatorBase GetValidator(DependencyObject obj)
		{
			return (ValidatorBase)obj.GetValue(ValidatorProperty);
		}

		public static void SetValidator(DependencyObject obj, ValidatorBase value)
		{
			obj.SetValue(ValidatorProperty, value);
		}

		public static readonly DependencyProperty ValidatorProperty =
			DependencyProperty.RegisterAttached("Validator", typeof(ValidatorBase), typeof(ValidatorService), new PropertyMetadata(ValidatorsPropertyChangedCallback));

		private static void ValidatorsPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var v = e.NewValue as ValidatorBase;
		    if (v != null)
		    {
		        if (String.IsNullOrEmpty(v.ManagerName))
		        {
                    System.Diagnostics.Debug.WriteLine("É necessário informar a Propriedade 'ManagerName' para o Validador");
                    throw new Exception("É necessário informar a Propriedade 'ManagerName' para o Validador");
		        }

		        v.Initialize(d as FrameworkElement);
		    }
		}
	}
}
